/**
 * author: Hanflyee  flyee1980@163.com
 * vision: v1.0
 * data: 2009-7-25
 */
package com.flyee.controller{
	
	import com.flyee.controller.timer.CartoonTimer;
	import com.flyee.controller.tools.cartoon.EquipManager;
	import com.flyee.io.Config;
	import com.flyee.model.Charactor;
	import com.flyee.service.pool.ObjectCollection;
	import com.flyee.service.pool.RelationMV;
	import com.flyee.utils.Constants;
	import com.flyee.utils.EventUtils;
	import com.flyee.utils.MyLog;
	import com.flyee.utils.Obsevable;
	import com.flyee.utils.Obsever;
	import com.flyee.utils.load.StackLoader;
	import com.flyee.utils.load.model.LoaderModel;
	import com.flyee.utils.load.processor.LoadLevelType;
	import com.flyee.utils.load.processor.LoadReturnType;
	import com.flyee.viewer.CharactorViewer;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.core.Application;
	
	/**
	 * 人物动画控制器 ， 控制动画TIMER
	 */
	public class CharacterCartoonController extends Obsevable implements Obsever{
		
		[Inject]
		public var equipManager : EquipManager;
		
		public function CharacterCartoonController(){
			
		}
		
		public function execute(o : Obsevable, command : String=null, args : * = null) : void {
			switch(command){
				case "changeActionDirect" : 
					changeActionDirect(args[0], args[1], args[2]);
					break ;
				default :
					break;
			}
		}
		
		/**
		 * 初始化玩家Viewer 
		 */
		public function initCharacterViewer(char : Charactor) : CharactorViewer {
			var charViewer : CharactorViewer = new CharactorViewer();
			charViewer.setNameText(char.name);
//			var bmp : Bitmap = new Bitmap(EffectDepot.loadingPlayer.bitmapData);
//			bmp.x = -10;
//			bmp.y = 30;
//			charViewer.charactorContainer.addChild(bmp);
//			charViewer.scaleX = 0.5;
//			charViewer.scaleY = 0.5;			
			if(ObjectCollection.master == char){
				ObjectCollection.masterViewer = charViewer;
				/**加载主角光环标记*/
//				charViewer.addChildAt(EffectDepot.masterTag, 0);
//				EffectDepot.masterTag.x = 5;
//				EffectDepot.masterTag.y = -5;
			}else{
				//将人物Viewer加入到Viewer对象集合
				ObjectCollection.charactorViewerMap.put(charViewer.vid, charViewer);
				//保存人物数据和人物Viewer的对应关系
				ObjectCollection.relationArray.push(new RelationMV(char.mid, charViewer.vid));				
			}	
			loadCharacterPlate(char, charViewer);	
			return charViewer;
		}

		/**
		 * 加载玩家Viewer动作图板 
		 */
		public function loadCharacterPlate(char : Charactor, charViewer : CharactorViewer, changeId : String = null) : void {
//			equipManager.timer.start();
			if(changeId)
				ObjectCollection.master.equipID = changeId;
			equipManager.getEquip(char.gcode, charViewer.plateArray, loadCharacterPlateHandler, changeId);
			
		}
		
		public function loadCharacterPlateHandler(id : String) : void {			
			var char : Charactor = ObjectCollection.getCharacter(id);
			var charViewer : CharactorViewer = ObjectCollection.getCharacterViewerByMid(char.mid);

			var cartoonTimer : CartoonTimer = null;
			if(!ObjectCollection.cartoonTimerMap.containsKey(charViewer.vid)){
				cartoonTimer = new CartoonTimer(char, charViewer, Constants.FRAME_ACTION_TIME);
				ObjectCollection.cartoonTimerMap.put(charViewer.vid, cartoonTimer);
			}else{
				cartoonTimer = ObjectCollection.cartoonTimerMap.get(charViewer.vid);
			}

			cartoonTimer.autoPlay = true;
			cartoonTimer.setCurrentStatus(1,0,0);
			cartoonTimer.timerHandle();	
			charViewer.nameText.x = charViewer.width / 2 - charViewer.nameText.width / 2;	
		}
		
		/**
		 * 改变玩家 动作，方向 
		 */
		public function changeActionDirect(charViewer : CharactorViewer, action : int, direct : int = -1, frame : int = -1) : void {
			if(charViewer.plateArray.length == 0) return ;
			if(!ObjectCollection.cartoonTimerMap.containsKey(charViewer.vid)) return ;
			var cartoonTimer : CartoonTimer = ObjectCollection.cartoonTimerMap.get(charViewer.vid);
			cartoonTimer.setCurrentStatus(action
										, (direct == -1) ? cartoonTimer.getDirect() : direct+1
										, (frame == -1) ? cartoonTimer.getFrame() : frame);
		} 
		
		/**
		 * 设置其他玩家可见 
		 */
		public function setPlayersVisible(value : Boolean) : void {
			for each(var charViewer : CharactorViewer in ObjectCollection.charactorViewerMap.values()){
				if(value == charViewer.playerVisible) continue;
				setPlayerVisible(value, charViewer);
			}
		}
		
		public function setPlayerVisible(value : Boolean, charViewer : CharactorViewer) : void {
			charViewer.playerVisible = value;
			charViewer.charactorContainer.visible = value;			
			var cartoonTimer : CartoonTimer = ObjectCollection.cartoonTimerMap.get(charViewer.vid) as CartoonTimer;
			if(value){
				var char : Charactor = ObjectCollection.charactorMap.get(ObjectCollection.getMidByVid(charViewer.vid));
				this.loadCharacterPlate(char, charViewer);
				cartoonTimer.autoPlay = value;
			}else{				
				cartoonTimer.autoPlay = value;
				if(charViewer.charactorContainer.numChildren != 0)
					charViewer.charactorContainer.removeChildAt(0);
				charViewer.dispose();
			}
		}

	}
	
}